package com.example.lib_audio.activity

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.lib_audio.R
import com.example.lib_audio.audio.AudioController
import com.example.lib_audio.databinding.ActivityMusicPlayBinding
import com.example.lib_audio.eventbus.AudioChangeProgressEvent
import com.example.lib_audio.eventbus.AudioLoadEvent
import com.example.lib_audio.eventbus.AudioPauseEvent
import com.example.lib_audio.eventbus.AudioRestartRotateAnim
import com.example.lib_audio.eventbus.AudioStartEvent
import com.example.lib_audio.tools.AudioAnimTool
import com.example.lib_audio.tools.ImageTool
import com.example.lib_audio.tools.NeedleAnimation
import com.example.lib_audio.tools.SongDuration
import com.example.lib_base.model.Song
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MusicPlayActivity : AppCompatActivity() {
    private lateinit var binding:ActivityMusicPlayBinding
    private lateinit var audioAnimTool: AudioAnimTool

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.statusBarColor = Color.parseColor("#3A2E45")

        binding = ActivityMusicPlayBinding.inflate(layoutInflater)
        setContentView(binding.root)

        audioAnimTool = AudioAnimTool(binding.albumView)

        //注册
        EventBus.getDefault().register(this)


        binding.closeView.setOnClickListener {
            finish()
        }

        //加载数据
        initUi(AudioController.getInstance()?.getCurrentPlaySong()!!)

        //暂停还是播放
        binding.playView.setOnClickListener {
            AudioController.getInstance()?.playOrPause()
        }

        //下一曲
        binding.nextView.setOnClickListener {
            AudioController.getInstance()?.nextSong()
        }

        //上一曲
        binding.previousView.setOnClickListener {
            AudioController.getInstance()?.previousSong()
        }

    }

    private fun initUi(song:Song){
        ImageTool.loadCircleImage(song.album,binding.albumView)
        binding.authorView.text = song.author
        binding.titleView.text = song.title
        audioAnimTool.start(0f, 360f)//开启动画
        binding.timeEndView.text = SongDuration.staticDuration(AudioController.getInstance()?.getDuration()!!)
        if(AudioController.getInstance()?.isPlaying() == true) {
            NeedleAnimation.startAnimation(binding.indicatorView)
            binding.playView.setImageResource(R.drawable.audio_stop)
        }else{
            audioAnimTool.pause()
            binding.playView.setImageResource(R.drawable.audio_play)
        }


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onPause(event: AudioPauseEvent){
        audioAnimTool.pause()
        NeedleAnimation.stopAnimation(binding.indicatorView)
        binding.playView.setImageResource(R.drawable.audio_play)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onStart(event: AudioStartEvent){
        audioAnimTool.resume()
        NeedleAnimation.startAnimation(binding.indicatorView)
        binding.playView.setImageResource(R.drawable.audio_stop)

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onLoad(event: AudioLoadEvent){
        audioAnimTool.start(0f, 360f)//开启动画
        ImageTool.loadCircleImage(event.song.album,binding.albumView)
        binding.authorView.text = event.song.author
        binding.titleView.text = event.song.title
        binding.playView.setImageResource(R.drawable.audio_stop)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun changeProgress(event: AudioChangeProgressEvent){
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun restartAnim(event: AudioRestartRotateAnim){
        audioAnimTool.start(0f,360f) //开启动画
    }

    override fun onDestroy() {
        super.onDestroy()
        //注销
        EventBus.getDefault().unregister(this)
    }

}